//
//  HMProductSizeTableViewCell.m
//  HMMarket
//
//  Created by maimaigo3 on 2017/5/18.
//  Copyright © 2017年 homai. All rights reserved.
//

#import "HMProductSizeTableViewCell.h"

#import "SearchModel.h"

#define width 50
#define height 28
#define Marin 13


@interface HMProductSizeTableViewCell ()

NS_STRONG UILabel *nameLabel;

@end

@implementation HMProductSizeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self setUpSubview];
        self.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    return self;
}

- (void)setUpSubview {
    _nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(20, 0, 70, 44)];
//    _nameLabel.text = @"规格:";
    [self addSubview:_nameLabel];
}

- (void)setDataDict:(NSDictionary *)dataDict {
    _dataDict = dataDict;
    _nameLabel.text = dataDict[@"attr_name"];
    NSString *dataString = dataDict[@"attr_value"];
    _dataArray = [dataString componentsSeparatedByString:@","];
    for (int i = 0; i < _dataArray.count; i++) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        [btn setTitle:_dataArray[i] forState:UIControlStateNormal];
        btn.layer.cornerRadius = 5;
        btn.clipsToBounds = YES;
        btn.layer.borderWidth = 2.0;
        [btn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(clickBtn:) forControlEvents:UIControlEventTouchUpInside];
        if (i == 0) {
            btn.layer.borderColor = [UIColor colorWithHexString:@"00994d"].CGColor;
        } else {
            btn.layer.borderColor = [UIColor colorWithHexString:@"eeeeee"].CGColor;
        }
        btn.tag = 100 + i;
        btn.frame = CGRectMake(CGRectGetMaxX(_nameLabel.frame) + width * i + Marin * (i+ 1), 8, width, height);
        [self addSubview:btn];
    }
    
}

- (void)clickBtn:(UIButton *)sender {
    for (int i = 0; i < _dataArray.count; i++) {
        UIButton *btn = (UIButton *)[self viewWithTag:100 + i];
        if (sender != btn) {
            btn.layer.borderColor = [UIColor colorWithHexString:@"eeeeee"].CGColor;
        }
    }
    
    sender.layer.borderColor = [UIColor colorWithHexString:@"00994d"].CGColor;
    if ([_delegate respondsToSelector:@selector(clickSize:attr_id:)]) {
        [_delegate clickSize:sender.titleLabel.text attr_id:_nameLabel.text];
    }
}

@end
